package com.example.scale;

import org.andengine.engine.camera.Camera;
import org.andengine.entity.scene.menu.MenuScene;
import org.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.andengine.entity.scene.menu.item.IMenuItem;
import org.andengine.entity.scene.menu.item.SpriteMenuItem;
import org.andengine.entity.scene.menu.item.decorator.ScaleMenuItemDecorator;
import org.andengine.entity.sprite.Sprite;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.util.GLState;

import com.example.scale.SceneManager.SceneType;

public class LevelSelectScene extends BaseScene implements
		IOnMenuItemClickListener {

	private MenuScene menuChildScene;
	public static int numLevels = 3;
	private SavedData mySavedData;
	private boolean levelCompleteArray[];

	// private LevelSelectBox levelSelectBox;

	@Override
	public void createScene() {
		levelCompleteArray = new boolean[LevelSelectScene.numLevels + 1];
		mySavedData = new SavedData(activity);
		mySavedData.loadProgressData();
		for(int i = 1; i <= LevelSelectScene.numLevels; i++) {
			levelCompleteArray[i-1] = mySavedData.levelCompleteArray[i-1];
		}
		
		//resourcesManager.level_select_background_region
		attachChild(new Sprite(GameActivity.CAMERA_WIDTH/2, GameActivity.CAMERA_HEIGHT/2,
				resourcesManager.blank_background_region, vbom) {
			@Override
			protected void preDraw(GLState pGLState, Camera pCamera) {
				super.preDraw(pGLState, pCamera);
				pGLState.enableDither();
			}
		});
		// levelSelectBox = new LevelSelectBox(0, 0, 650, 400,
		// ResourcesManager.getInstance().complete_numbers_region, vbom);
		// Background SelectBackground = new Background(Color.WHITE);
		// SelectBackground.setColor(0, 0, 0, 0.5f);
		// setBackground(SelectBackground);

		menuChildScene = new MenuScene(camera);
		menuChildScene.setPosition(0, 0);

		/*Text LevelSelectText = new Text(GameActivity.CAMERA_WIDTH / 2,
				GameActivity.CAMERA_HEIGHT - 30, resourcesManager.font,
				"Select a Level", vbom);
		LevelSelectText.setColor(Color.WHITE);
		attachChild(LevelSelectText);*/
		
		final Sprite backButton = new Sprite(45, GameActivity.CAMERA_HEIGHT-45, resourcesManager.arrow_region, vbom) {
			public boolean onAreaTouched(TouchEvent touchEvent, float X, float Y) {
				if (touchEvent.isActionDown()) {
					onBackKeyPressed();
				}
				return true;
			}
		};
		backButton.setScale(0.5f);
		attachChild(backButton);
		registerTouchArea(backButton);
		
		Sprite levelSelectHeader = new Sprite(GameActivity.CAMERA_WIDTH/2, GameActivity.CAMERA_HEIGHT-50,
				resourcesManager.level_select_header_region, vbom);
		//levelSelectHeader.setScale(.8f);
		attachChild(levelSelectHeader);
		
		final IMenuItem levelSelectItem[] = new ScaleMenuItemDecorator[numLevels+1];
		for(int i=1; i<=numLevels ; i++) {
			resourcesManager.complete_numbers_region.setCurrentTileIndex(i);
			levelSelectItem[i] = new ScaleMenuItemDecorator(
					new SpriteMenuItem(i, resourcesManager.complete_numbers_region,
							vbom), 1, .9f);
			if(!levelCompleteArray[i-1]) {
				levelSelectItem[i].setAlpha(0.5f);
			}
			menuChildScene.addMenuItem(levelSelectItem[i]);
		}

		/*resourcesManager.complete_numbers_region.setCurrentTileIndex(1);
		final IMenuItem level1SelectItem = new ScaleMenuItemDecorator(
				new SpriteMenuItem(1, resourcesManager.complete_numbers_region,
						vbom), 1, .9f);
		menuChildScene.addMenuItem(level1SelectItem);
		resourcesManager.complete_numbers_region.setCurrentTileIndex(2);
		final IMenuItem level2SelectItem = new ScaleMenuItemDecorator(
				new SpriteMenuItem(2, resourcesManager.complete_numbers_region,
						vbom), 1, .9f);
		menuChildScene.addMenuItem(level2SelectItem);
		resourcesManager.complete_numbers_region.setCurrentTileIndex(3);
		final IMenuItem level3SelectItem = new ScaleMenuItemDecorator(
				new SpriteMenuItem(3, resourcesManager.complete_numbers_region,
						vbom), 1, .9f);
		menuChildScene.addMenuItem(level3SelectItem);
		resourcesManager.complete_numbers_region.setCurrentTileIndex(4);
		final IMenuItem level4SelectItem = new ScaleMenuItemDecorator(
				new SpriteMenuItem(4, resourcesManager.complete_numbers_region,
						vbom), 1, .9f);
		menuChildScene.addMenuItem(level4SelectItem);
		resourcesManager.complete_numbers_region.setCurrentTileIndex(5);
		final IMenuItem level5SelectItem = new ScaleMenuItemDecorator(
				new SpriteMenuItem(5, resourcesManager.complete_numbers_region,
						vbom), 1, .9f);
		menuChildScene.addMenuItem(level5SelectItem);
		resourcesManager.complete_numbers_region.setCurrentTileIndex(6);
		final IMenuItem level6SelectItem = new ScaleMenuItemDecorator(
				new SpriteMenuItem(6, resourcesManager.complete_numbers_region,
						vbom), 1, .9f);
		menuChildScene.addMenuItem(level6SelectItem);
		resourcesManager.complete_numbers_region.setCurrentTileIndex(7);
		final IMenuItem level7SelectItem = new ScaleMenuItemDecorator(
				new SpriteMenuItem(7, resourcesManager.complete_numbers_region,
						vbom), 1, .9f);
		menuChildScene.addMenuItem(level7SelectItem);
		resourcesManager.complete_numbers_region.setCurrentTileIndex(8);
		final IMenuItem level8SelectItem = new ScaleMenuItemDecorator(
				new SpriteMenuItem(8, resourcesManager.complete_numbers_region,
						vbom), 1, .9f);
		menuChildScene.addMenuItem(level8SelectItem);
		resourcesManager.complete_numbers_region.setCurrentTileIndex(9);
		final IMenuItem level9SelectItem = new ScaleMenuItemDecorator(
				new SpriteMenuItem(9, resourcesManager.complete_numbers_region,
						vbom), 1, .9f);
		menuChildScene.addMenuItem(level9SelectItem);*/

		menuChildScene.buildAnimations();
		menuChildScene.setBackgroundEnabled(false);

		// X-positions need to have pattern: 3/16, 8/16, 13/16, 3/16
		// Y-positions need to have pattern: 11/16, 11/16, 11/16, 7/16, ..., 3/16, ...
		for(int i=1; i<numLevels+1; i++) {
			levelSelectItem[i].setPosition(((5*(((i+2)%3)+1))-2) * GameActivity.CAMERA_WIDTH / 16,
					(11-(4*((i-1)/3))) * GameActivity.CAMERA_HEIGHT / 16);
		}
		
		/*level1SelectItem.setPosition(3 * GameActivity.CAMERA_WIDTH / 16,
				11 * GameActivity.CAMERA_HEIGHT / 16);
		level2SelectItem.setPosition(GameActivity.CAMERA_WIDTH / 2,
				11 * GameActivity.CAMERA_HEIGHT / 16);
		level3SelectItem.setPosition(13 * GameActivity.CAMERA_WIDTH / 16,
				11 * GameActivity.CAMERA_HEIGHT / 16);
		level4SelectItem.setPosition(3 * GameActivity.CAMERA_WIDTH / 16,
				7 * GameActivity.CAMERA_HEIGHT / 16);
		level5SelectItem.setPosition(GameActivity.CAMERA_WIDTH / 2,
				7 * GameActivity.CAMERA_HEIGHT / 16);
		level6SelectItem.setPosition(13 * GameActivity.CAMERA_WIDTH / 16,
				7 * GameActivity.CAMERA_HEIGHT / 16);
		level7SelectItem.setPosition(3 * GameActivity.CAMERA_WIDTH / 16,
				3 * GameActivity.CAMERA_HEIGHT / 16);
		level8SelectItem.setPosition(GameActivity.CAMERA_WIDTH / 2,
				3 * GameActivity.CAMERA_HEIGHT / 16);
		level9SelectItem.setPosition(13 * GameActivity.CAMERA_WIDTH / 16,
				3 * GameActivity.CAMERA_HEIGHT / 16);*/

		menuChildScene.setOnMenuItemClickListener(this);
		setChildScene(menuChildScene);
	}

	@Override
	public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem,
			float pMenuItemLocalX, float pMenuItemLocalY) {
		if(levelCompleteArray[pMenuItem.getID()-1]) {
			// switch (pMenuItem.getID()) {
			// case LVL_1_0:
			if(pMenuItem.getID()==1) {
				SceneManager.getInstance().whichLevel = 1;
			} else if(pMenuItem.getID()==2) {
				SceneManager.getInstance().whichLevel = 2;
			} else if(pMenuItem.getID()==9) {
				SceneManager.getInstance().whichLevel = 9;
			} else {
				SceneManager.getInstance().whichLevel = 3;
			}
			SceneManager.getInstance().loadGameScene(engine);
		}
		return true;
		// default:
		// return false;
		// }
	}
	
	@Override
	public void onBackKeyPressed() {
		SceneManager.getInstance().loadMenuScene(engine);
		SceneManager.getInstance().levelSelectScene.disposeScene();
		ResourcesManager.getInstance().unloadLevelSelectTextures();
	}

	@Override
	public SceneType getSceneType() {
		return SceneType.SCENE_LEVEL_SELECT;
	}

	@Override
	public void disposeScene() {
		// TODO Auto-generated method stub

	}

}
